/**
 * Project: ameba.web
 * 
 * File Created at 2015年12月10日
 * $Id$
 * 
 * Copyright 2015 dface.cn Croporation Limited.
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of
 * dface Company. ("Confidential Information").  You shall not
 * disclose such Confidential Information and shall use it only in
 * accordance with the terms of the license agreement you entered into
 * with www.dface.cn
 */
package cn.dface.ameba.web.controller;

import java.io.IOException;
import java.util.List;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import cn.dface.ameba.biz.service.StorageService;
import cn.dface.ameba.biz.vo.BucketVo;
import cn.dface.ameba.biz.vo.ResourceVo;
import cn.dface.ameba.biz.vo.UserVo;
import cn.dface.ameba.toolkit.util.SessionUtils;
import cn.dface.ameba.web.vo.ResponseResult;
import cn.dface.framework.core.enums.ResultEnums;
import cn.dface.framework.core.page.PageModel;
import cn.dface.framework.core.vo.ResultVo;

/**
 * StorageController
 * 
 * @author LVZY
 */
@RestController
@RequestMapping("/storage")
public class StorageController {

    @Autowired
    private StorageService storageService;

    @RequestMapping(value = "/bucket/{bucketId}/resource/page/list")
    public PageModel getPage(@PathVariable(value = "bucketId") Integer bucketId,
                             @RequestParam(value = "descKey", required = false) String descKey,
                             @RequestParam(value = "pageNo") Integer pageNo,
                             @RequestParam(value = "pageSize") Integer pageSize) {
       return this.storageService.getPageOfResource(bucketId, descKey, pageNo, pageSize);
    }
    
    @RequestMapping(value = "/bucket/{bucketId}/resource/page/count")
    public Integer getTotalPageValue(@PathVariable(value = "bucketId") Integer bucketId,
                                     @RequestParam(value = "descKey", required = false) String descKey,
                                     @RequestParam(value = "pageSize") Integer pageSize) {
        return this.storageService.countInBucket(bucketId, descKey, pageSize);
    }

    @RequestMapping(value = "/bucket/list")
    public List<BucketVo> getAllBucket() {
        return this.storageService.getAllOfBucket();
    }

    @RequestMapping(value = "/bucket/action/add", method = RequestMethod.POST)
    public ResponseResult addBucket(@RequestParam(value = "name") String name,
                                    @RequestParam(value = "url") String url,
                                    @RequestParam(value = "desc") String desc) {
        ResponseResult result = new ResponseResult();
        result.setSucceeded(true);
        BucketVo bucket = new BucketVo();
        bucket.setName(name);
        bucket.setDesc(desc);
        bucket.setUrl(url);
        try {
            result.setObj(this.storageService.save(bucket));
        } catch (Exception e) {
            result.setSucceeded(false);
            result.setMessage(e.getMessage());
        }
        return result;
    }

    @RequestMapping(value = "/bucket/id/{id}", method = RequestMethod.POST)
    public ResponseResult updateBucket(@PathVariable(value = "id") Integer id,
                                       @RequestParam(value = "name") String name,
                                       @RequestParam(value = "url") String url,
                                       @RequestParam(value = "desc") String desc) {
        ResponseResult result = new ResponseResult();
        result.setSucceeded(true);
        BucketVo bucket = new BucketVo();
        bucket.setSid(id);
        bucket.setName(name);
        bucket.setDesc(desc);
        bucket.setUrl(url);
        try {
            this.storageService.updateBucket(bucket);
        } catch (Exception e) {
            result.setSucceeded(false);
            result.setMessage(e.getMessage());
        }
        return result;
    }

    @RequestMapping(value = "/bucket/{bucketId}/resource/action/upload", method = RequestMethod.POST)
    public ResponseResult upload(@PathVariable(value = "bucketId") Integer bucketId,
                                 @RequestParam(value = "description", required = true) String description,
                                 @RequestParam(value = "file", required = true) MultipartFile file,
                                 HttpSession session) {
        ResponseResult result = new ResponseResult();
        result.setSucceeded(true);
        try {
            ResourceVo res = new ResourceVo();
            res.setName(file.getOriginalFilename());
            res.setDescription(description);
            res.setData(file.getBytes());
            res.setBucket(this.storageService.getOneOfBucket(bucketId));
            res.setUser((UserVo) session.getAttribute(SessionUtils.USER));
            
            ResultVo retVo = this.storageService.sendToQiniuAndSaveToDB(res);
            if (retVo.getStatus() == ResultEnums.SUCESS.ordinal()) {
                result.setObj(retVo.getObject());
            } else {
                result.setSucceeded(false);
                result.setMessage(retVo.getErrorMsg());
            }
        } catch (IOException e) {
            result.setSucceeded(false);
            result.setMessage(e.getMessage());
        }
        return result;
    };

}
